package net.frankish.moviebank2.front;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.frankish.moviebank.back.db.DerbyDatabase;
import net.frankish.moviebank.beans.Movie;
import net.frankish.moviebank2.front.model.MovieTable;

public class MovieListingPanel extends JPanel{

	private static final long serialVersionUID = 1L;

	private static final URL urlEdit = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/32_32_tools.png" );
	private static final URL urlRemove = MovieBankMenu.class.getResource( "/net/frankish/moviebank/images/128_128_bomb.png" );
	
	
	//-- Swing components
	private MovieTable movieTable = null;
	private MoviePanel movieDetail = null;
	
	private JButton edit = null;
	private JButton remove = null;
	private JSplitPane splitPane = null;

	//-- Data components
	private Vector<Movie> movies = null;
	
	private static Comparator<Movie> movieComparator = new Comparator<Movie>(){
		public int compare(Movie a, Movie b) {
			if(a.getTitle() == null || b.getTitle() == null)
				return 0;
			return a.getTitle().compareTo(b.getTitle());
		}
	};

	private static JPanel instance;

	public MovieListingPanel() {
		movies = DerbyDatabase.findAllMovie();
		Collections.sort(movies,movieComparator);
		movieTable = new MovieTable(movies);
		movieDetail = new MoviePanel();
		this.setLayout(new GridBagLayout());
		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
		splitPane.setOneTouchExpandable(true);
		splitPane.setTopComponent(new JScrollPane(movieTable));
		splitPane.setBottomComponent(new JScrollPane(movieDetail));
		splitPane.setDividerLocation(250);
		edit = new JButton(new ImageIcon(new ImageIcon(urlEdit).getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH)));
		edit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				editSelection();
			}
		});
		remove = new JButton(new ImageIcon(new ImageIcon(urlRemove).getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH)));
		remove.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				removeSelection();
			}
		});
		this.add(edit,new GridBagConstraints(0,0,1,1,0.0001, 0.00001, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 1, 1));
		this.add(remove,new GridBagConstraints(2,0,1,1,0.00001, 0.00001, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 1, 1));
		this.add(splitPane,new GridBagConstraints(0,1,3,1,1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 1, 1));
		movieTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting())return;
				int sortedRow = movieTable.getRowSorter().convertRowIndexToModel(movieTable.getSelectedRow());
				movieDetail.setMaximumSize(new Dimension(splitPane.getSize().width - 5, (splitPane.getSize().height - 5)/2));
				movieDetail.load(movies.get(sortedRow));
			}
		});
	}

	protected void removeSelection() {
		
	}

	protected void editSelection() {
		try{
			int sortedRow = movieTable.getRowSorter().convertRowIndexToModel(movieTable.getSelectedRow());
			movieDetail.setMaximumSize(new Dimension(splitPane.getSize().width - 5, (splitPane.getSize().height - 5)/2));
			movieDetail.load(movies.get(sortedRow));
			movieDetail.setEditable(true);
		}catch(Exception ex){
			Logger.getLogger(this.getClass().getName()).log(Level.WARNING,"Exception editing",ex);
		}
	}

	public static JPanel getInstance() {
		if(instance == null)
			instance = new MovieListingPanel();
		return instance;
	}
}
